import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main
{
    public static int N = 1010;
    public static int[] dx = {0, 0, 1, -1};
    public static int[] dy = {-1, 1, 0, 0};
    public static int n, m, x1, y1, x2, y2;
    public static char[][] arr = new char[N][N];
    public static int[][] dist = new int[N][N]; // 标记当前位置有没有搜索过，以及⾛到该位置时候的最短步数
    public static int bfs() {
        if(arr[x2][y2] == '*')
            return -1;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                dist[i][j] = -1; // 表明刚开始每个位置都没有搜索过
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{x1, y1});
        dist[x1][y1] = 0;
        while(!q.isEmpty()) {
            int[] t = q.poll();
            int a = t[0], b = t[1];
            for(int i = 0; i < 4; i++) {
                int x = a + dx[i], y = b + dy[i];
                if(x >= 1 && x <= n && y >= 1 && y <= m && arr[x][y] == '.' && dist[x][y] == -1) {
                    q.add(new int[]{x, y});
                    dist[x][y] = dist[a][b] + 1;
                    if(x == x2 && y == y2) {
                        return dist[x][y];
                    }
                }
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        x1 = in.nextInt();
        y1 = in.nextInt();
        x2 = in.nextInt();
        y2 = in.nextInt();
        for(int i = 1; i <= n; i++) {
            String tmp = in.next();
            for(int j = 1; j <= m; j++) {
                arr[i][j] = tmp.charAt(j - 1);
            }
        }
        System.out.println(bfs());
    }
}
